/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */
 
function FormUtils(){
	this.jsjava_class="jsorg.eob.document.FormUtils";
}

/**
 * Get checked count of a checkbox group
 * @param form
 * @param checkboxName
 */
FormUtils.getCheckedCount=function(form,checkboxName){
	var cObj=form.elements[checkboxName];
	if(!cObj){
		return 0;
	}
	if(cObj.length){
		var count=0;
		for(var i=0;i<cObj.length;i++){
			if(cObj[i].checked){
				count++;
			}
		}
		return count;
	}
	if(cObj.checked){
		return 1;
	}
	return 0;
};

/**
 * Get checked count of a select box
 * @param form
 * @param selectName
 */
FormUtils.getSelectedCount=function(form,selectName){
	var sObj=form.elements[selectName];
	if(!sObj){
		return 0;
	}
	var count=0;
	for(var i=0;i<sObj.options.length;i++){
		if(sObj.options[i].selected){
			count++;
		}
	}
	return count;
};

/**
 * Set state checked of each items of the checkbox group
 * @param form
 * @param checkboxName
 */
FormUtils.checkAllCheckbox=function(form,checkboxName){
	var cObj=form.elements[checkboxName];
	if(!cObj){
		return;
	}
	if(cObj.length){
		for(var i=0;i<cObj.length;i++){
			cObj[i].checked=true;
		}
	}else{
		cObj.checked=true;
	}
};

/**
 * Set state unchecked of each items of the checkbox group
 * @param form
 * @param checkboxName
 */
FormUtils.unCheckAllCheckbox=function(form,checkboxName){
	var cObj=form.elements[checkboxName];
	if(!cObj){
		return;
	}
	if(cObj.length){
		for(var i=0;i<cObj.length;i++){
			cObj[i].checked=false;
		}
	}else{
		cObj.checked=false;
	}
};

/**
 * Set state selected of each items of the select box
 * @param form
 * @param selectName
 */
FormUtils.selectAll=function(form,selectName){
	var sObj=form.elements[selectName];
	if(!sObj){
		return;
	}
	if(sObj.multiple){
		for(var i=0;i<sObj.options.length;i++){
			sObj.options[i].selected=true;
		}
	}
};

/**
 * Set state selected of indexed items of the select box
 * @param form
 * @param selectName
 * @param indexes
 */
FormUtils.selectByIndexes=function(form,selectName,indexes){
	var sObj=form.elements[selectName];
	if(!sObj){
		return;
	}
	var options=sObj.options;
	if(sObj.multiple){		
		for(var i=0;i<indexes.length;i++){
			var index=indexes[i];
			if(index<options.length){
				options[index].selected=true;
			}
		}
	}else{
		var index=indexes[0];
		if(index<options.length){
			options[index].selected=true;
		}
	}
};

/**
 * Set state selected of valued items of the select box
 * @param form
 * @param selectName
 * @param values
 */
FormUtils.selectByValues=function(form,selectName,values){
	var sObj=form.elements[selectName];
	if(!sObj){
		return;
	}
	var options=sObj.options;
	if(sObj.multiple){		
		for(var i=0;i<options.length;i++){
			var value=options[i].value;
			if(ArrayUtils.indexOf(values,value)!=-1){
				options[i].selected=true;
			}
		}
	}else{
		var firstValue=values[0];
		for(var i=0;i<options.length;i++){
			var value=options[i].value;
			if(value!=undefined&&value==firstValue){
				options[i].selected=true;
			}
		}
	}
};


/**
 * Get checked values of the checkbox group
 * @param form
 * @param checkboxName
 */
FormUtils.getCheckedValues=function(form,checkboxName){
	var cObj=form.elements[checkboxName];
	if(!cObj){
		return;
	}
	var arr=new Array();
	if(cObj.length){
		for(var i=0;i<cObj.length;i++){
			if(cObj[i].checked){
				arr[arr.length]=cObj[i].value;
			}
		}
	}else{
		if(cObj.checked){
			arr[0]=cObj.value;
		}
	}
	return arr;
};

/**
 * Get checked values of the radio group
 * @param form
 * @param radioName
 */
FormUtils.getRadioValue=function(form,radioName){
	var cObj=form.elements[radioName];
	if(!cObj){
		return;
	}
	var value;
	if(cObj.length){
		for(var i=0;i<cObj.length;i++){
			if(cObj[i].checked){
				value=cObj[i].value;
				break;
			}
		}
	}else{
		if(cObj.checked){
			value=cObj.value;
		}
	}
	return value;
};

/**
 * Get selected values of the checkbox group
 * @param form
 * @param selectName
 */
FormUtils.getSelectedValues=function(form,selectName){
	var sObj=form.elements[selectName];
	if(!sObj){
		return;
	}
	var arr=new Array();
	var options=sObj.options;
	for(var i=0;i<options.length;i++){
		var option=options[i];
		if(option.selected){
			arr[arr.length]=option.value;
		}
	}
	return arr;
};